package com.safeneeds.util.net;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import com.safeneeds.util.base.RetType;


/**
 * 网络连接工具类
 * @author gaof
 * @date 2015年10月30日
 */
public class HttpClientServer {

	public static String sendHttpPost(String url, MultipartEntity entity){
		
		HttpClient httpClient = initHttpClient();
		
		HttpPost httpPost = new HttpPost(url);
		//发送http请求
		httpPost.setEntity(entity);
		
		try {
			HttpResponse response = httpClient.execute(httpPost);
			
			//请求成功
	        if (response.getStatusLine().getStatusCode() == 200) {
	        	HttpEntity retEntity = response.getEntity();
	        	
				return EntityUtils.toString(retEntity);
				
			}else{
				RetType retError = new RetType();
				retError.setErrMsg("接口服务器请求失败");
				httpClient.getConnectionManager().shutdown();
				httpClient = null;
				return com.alibaba.fastjson.JSON.toJSONString(retError);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			
			RetType retError = new RetType();
			retError.setErrMsg("系统出错");
			httpClient.getConnectionManager().shutdown();
			httpClient = null;
			return com.alibaba.fastjson.JSON.toJSONString(retError);
		} 
		
	}

	/**
	 * 初始化httpClient
	 */
	private static HttpClient initHttpClient() {
		
		HttpParams params = new BasicHttpParams();
		HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);// http协议版本
		HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);// 数据编码

		// 设置连接超时和 Socket 超时，以及 Socket 缓存大小
		HttpConnectionParams.setConnectionTimeout(params, 10 * 1000);
		HttpConnectionParams.setSoTimeout(params, 10 * 1000);
		HttpConnectionParams.setSocketBufferSize(params, 8192);
		// 设置重定向，缺省为 true
		HttpClientParams.setRedirecting(params, true);
		// 注册协议端口
		SchemeRegistry schReg = new SchemeRegistry();
		schReg.register(new Scheme("http", PlainSocketFactory
				.getSocketFactory(), 80));
		schReg.register(new Scheme("https",
				SSLSocketFactory.getSocketFactory(), 443));

		ClientConnectionManager conMgr = new ThreadSafeClientConnManager(
				params, schReg);
		
		return new DefaultHttpClient(conMgr, params);
		
	}
	
}
